<?php
//including header.php 
include '../../include/header.php';
//including page.php (It is a class which contain functions)
include '../../libraries/page.php';
//created object for page.php
$page = new page();
session_start();

// checking the session
if (!$_SESSION['username']) {
    header("location:../user/login.php");
}
if ($_GET['q'] == 'logout') {
    $page->user_logout();
    header("location:../user/login.php");
}

// checking the data posted or not
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if ($_POST['chkDel'] == "") {
        $msg = $message[8];
    } else {
        $result = $page->delete("productid", "products", $_POST['chkDel']);
        if ($result) {
            $msg = $message[2];
        } else {
            $msg = $message[0];
        }
    }
}
if ($_GET['status']) {
    $msg = $message[3];
}
$products = 'active';
include '../../include/navbar.php';
?>

<!------------------css style sheets for responsive tables------------------------------------>
<link rel="stylesheet" type="text/css" href="../../css/products/style_table.css">
<link rel="stylesheet" type="text/css" href="../../css/products/responsive_table.css">

<?
// start the buffer
ob_start();
?>
<br>

<!----------------------------------Display the result message of deletion--------------------->
<div  align="center" style="margin-top: 10px;"><? echo $msg; ?></div>

<!-------------------------------------------Group selection options----------------->
<div class="group_select_desk">
    <span><strong style="font-size:22px;">Products</strong>
        <!---  <a onclick="javascript:checkAll('frmMain', false);" href="javascript:void();" style="float:right">uncheck all</a>
          <a onclick="javascript:checkAll('frmMain', true);" href="javascript:void();" style="float:right; margin-right: 20px;">check all</a></span>-->
        <a href="javascript:reserveCheck('chkDel[]')" style="float:right">Invert Select</a>
        <a href="javascript:allCheck('chkDel[]',false)" style="float:right; margin-right: 20px;">Select None</a> 
        <a href="javascript:allCheck('chkDel[]',true)" style="float:right; margin-right: 20px;">Select All</a></span>
</div>
<div class="group_select_mob">
    <span><strong style="font-size:22px;">Products</strong>
        <div class="btn-group"  style="float: right">
            <a class="btn dropdown-toggle" data-toggle="dropdown" href="#" >
                Select&nbsp;
                <span class="caret"></span>
            </a>
            <ul class="dropdown-menu"  style="float:left;margin-left: -84px">
                <!-- dropdown menu links -->
                <li><a href="javascript:allCheck('chkDel[]',true)">Select All</a></li>
                <li><a href="javascript:allCheck('chkDel[]',false)">Select None</a></li> 
                <li><a href="javascript:reserveCheck('chkDel[]')" >Invert Select</a></li>

            </ul>
        </div>



    </span>
</div>
<br />

<!------------------------------Form------------------------------------->
<form name="frmMain" action="" method="post" OnSubmit="return onDelete();">

    <!------------------------------Details Table------------------------------------->
    <table  class="table1 table-striped" style="margin-left: auto;margin-right: auto; ">
        <thead  class="thead1">
            <tr>
                <th>check</th>
                <th>SL NO</th>
                <th>Name</th>
                <th>Code</th>
                <th>Part No</th>
                <th>Packing</th>
                <th>Price</th>
                <th>Opqty</th>
                <th>Group</th>
                <th>Edit</th>
            </tr>
        </thead>
        <tbody>
            <?
            //php code to get the data from products table 
            $products = mysql_query("select * from products");
            $id = 0;
            while ($products_data = mysql_fetch_array($products)) {

            $product_id = $products_data['productid'];
            $product_group_id = $products_data['groupid'];
            $name = ucwords(strtolower($products_data['name']));
            $code = $products_data['code'];
            $oldpartno = $products_data['oldpartno'];
            $partno = $products_data['partno'];
            $packing = $products_data['packing'];
            $price = $products_data['price'];
            $opqty = $products_data['opqty'];
            $active = $products_data['active'];
            $groups_actual = mysql_query("select * from groups where groupid = $product_group_id");
            $groups_actual_data = mysql_fetch_array($groups_actual);
            //$groups_actual_id = $groups_actual_data['groupid'];
            $groups_name = ucwords(strtolower($groups_actual_data['name']));
            $id++;
            ?>

            <!-------------------------Dynamic row to display from database--------------------->
            <tr>
                <td><input type="checkbox" name="chkDel[]" id="chkDel<?= $i; ?>" value="<?= $product_id; ?>"></td>
                <td><?= $id; ?></td>
                <td><a href="product_update?id=<?= $product_id; ?>"><?= $name; ?></a></td>
                <td><?= $code; ?></td>
                <td><?= $partno; ?></td>
                <td><?= $packing; ?></td>
                <td><?= $price; ?></td>
                <td><?= $opqty; ?></td>
                <td><?= $groups_name; ?></td>
                <td><a href="product_update?id=<?= $product_id; ?>">Edit</a></td>
            </tr>

            <?
            //php code to get the data from products table 
            $c_products = mysql_query("select p.*
            from products p
            join combinations c
            on c.c_product_id = p.productid
            where c.productid = $product_id");
            while ($c_products_data = mysql_fetch_array($c_products)) {
            $c_product_id = $products_data['productid'];
            $c_product_group_id = $c_products_data['groupid'];
            $c_name = ucwords(strtolower($c_products_data['name']));
            $c_code = $c_products_data['code'];
            $c_oldpartno = $c_products_data['oldpartno'];
            $c_partno = $c_products_data['partno'];
            $c_packing = $c_products_data['packing'];
            $c_price = $c_products_data['price'];
            $c_opqty = $c_products_data['opqty'];
            $c_active = $c_products_data['active'];
            $c_groups_actual = mysql_query("select * from groups where groupid = $c_product_group_id");
            $c_groups_actual_data = mysql_fetch_array($c_groups_actual);
            //$c_groups_actual_id = $c_groups_actual_data['groupid'];
            $c_groups_name = ucwords(strtolower($c_groups_actual_data['name']));
            ?>

            <tr>
                <td>&nbsp;</td>
                <td></td>
                <td><a href="product_update?id=<?= $product_id; ?>"><i style="font-size: 12px;color:#333"><b><?= $c_name; ?></b></i></a></td>
                <td><i style="font-size: 12px;color:#333"><b><?= $c_code; ?></b></i></td>
                <td><i style="font-size: 12px;color:#333"><b><?= $c_partno; ?></b></i></td>
                <td><i style="font-size: 12px;color:#333"><b><?= $c_packing; ?></b></i></td>
                <td><i style="font-size: 12px;color:#333"><b><?= $c_price; ?></b></i></td>
                <td><i style="font-size: 12px;color:#333"><b><?= $c_opqty; ?></b></i></td>
                <td><i style="font-size: 12px;color:#333"><b><?= $c_groups_name; ?></b></i></td>
                <td><a href="product_update?id=<?= $c_product_id; ?>"><i style="font-size: 12px;color:#333"><b>Edit</b></i></a></td>
            </tr>

            <?
            }
            ?>

            <?
            // Closing the loop
            }

            ?>
        </tbody>
    </table>

    <!----------------------------CRUD Buttons-------------------------------------->
    <div align="center" style="margin-top:10px">
        <a href="product_add"><button class="btn btn-primary" type="button">Add</button></a>
        <button class="btn btn-danger" type="submit">Delete</button>

    </div>
</form>

<?php
//including footer.php
include '../../include/footer.php';
// flush the buffer
flush();
?>